//
//  wonderfulTableViewController.m
//  Travelmemory
//
//  Created by lanou3g on 16/3/30.
//  Copyright © 2016年 ABCD. All rights reserved.
//

#import "wonderfulTableViewController.h"
#import "TravelHelper.h"
#import "WoderfulTableViewCell.h"
#import "WoderfulCollectionViewCell.h"
#import "WoderfulModel.h"
#import "UIImageView+WebCache.h"
@interface wonderfulTableViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property(nonatomic,strong)NSMutableArray *wonderfulArray;

@property(nonatomic,strong)NSMutableArray *ImageArray;

@property(nonatomic,strong)NSMutableDictionary *imageDic;

//接受协议
@end

@implementation wonderfulTableViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.wonderfulArray = [NSMutableArray array];
    
    [self hadel];
    /** 注册 */
    [self.tableView registerNib:[UINib nibWithNibName:@"WoderfulTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"woderfulCell"];
    
    
    
    //    NSLog(@"%@",self.nameId);
}

//去哪拍: http://nahaowan.com/api/v2/haowan/list?pagesize=20&collection_id=443&geo=0.0%2C0.0&sort=DESC&location=%E5%8C%97%E4%BA%AC&recommended=1&list=collection&offset=0&section=%E7%B2%BE%E9%80%89&

//古镇: http://nahaowan.com/api/v2/haowan/list?pagesize=20&collection_id=441&geo=0.0%2C0.0&sort=DESC&location=%E5%8C%97%E4%BA%AC&recommended=1&list=collection&offset=0&section=%E7%B2%BE%E9%80%89&

//民 : http://nahaowan.com/api/v2/haowan/list?pagesize=20&collection_id=437&geo=0.0%2C0.0&sort=DESC&location=%E5%8C%97%E4%BA%AC&recommended=1&list=collection&offset=0&section=%E7%B2%BE%E9%80%89&

//PK: http://nahaowan.com/api/v2/haowan/list?pagesize=20&collection_id=426&geo=0.0%2C0.0&sort=DESC&location=%E5%8C%97%E4%BA%AC&recommended=1&list=collection&offset=0&section=%E5%A7%BF%E5%8A%BF%E7%A0%94%E7%A9%B6&

//海盗: http://nahaowan.com/api/v2/haowan/list?pagesize=20&collection_id=435&geo=0.0%2C0.0&sort=DESC&location=%E5%8C%97%E4%BA%AC&recommended=1&list=collection&offset=0&section=%E7%B2%BE%E9%80%89&

//温泉: http://nahaowan.com/api/v2/haowan/list?pagesize=20&collection_id=379&geo=0.0%2C0.0&sort=DESC&location=%E5%8C%97%E4%BA%AC&recommended=1&list=collection&offset=0&section=%E7%B2%BE%E9%80%89&

//购物: http://nahaowan.com/api/v2/haowan/list?pagesize=20&collection_id=438&geo=0.0%2C0.0&sort=DESC&location=%E5%8C%97%E4%BA%AC&recommended=1&list=collection&offset=0&section=%E7%B2%BE%E9%80%89&



//     http://nahaowan.com/api/v2/haowan/list?pagesize=20&collection_id=428&geo=0.0%2C0.0&sort=DESC&location=%E5%8C%97%E4%BA%AC&recommended=1&list=collection&offset=0&section=%E7%B2%BE%E9%80%89&

// 1.441 7.437 2.426 3.435 4.379 5.438 6.428
-(void)hadel{
    NSString *str1 = @"http://nahaowan.com/api/v2/haowan/list?pagesize=20&collection_id=";
    NSString *str2 = @"&geo=0.0%2C0.0&sort=DESC&location=%E5%8C%97%E4%BA%AC&recommended=1&list=collection&offset=0&section=%E7%B2%BE%E9%80%89&";
    self.imageDic = [NSMutableDictionary new];
    
    [[TravelHelper defaultTravelHelper]get:[NSString stringWithFormat:@"%@%@%@",str1,self.nameId,str2]  params:nil success:^(id responseObj) {
        
        for (NSDictionary *dict in responseObj[@"data"][@"list"]) {
            
            WoderfulModel *model = [[WoderfulModel alloc] init];
            
            [model setValuesForKeysWithDictionary:dict];
            model.headimg = dict[@"user"][@"headimg"];
            model.username = dict[@"user"][@"username"];
            model.urlArray = [NSMutableArray array];
            
            NSMutableArray *imageArray = [NSMutableArray array];
            [imageArray addObjectsFromArray:dict[@"photos"]];
            for (NSDictionary *dic in imageArray) {
                
                
                NSDictionary *dic1 = dic[@"image"];
                
                [model.urlArray addObject:dic1[@"url"]];
                
                
            }
            
            [self.wonderfulArray addObject:model];
            
        }[self.tableView reloadData];
        
    } failure:^(NSError *error) {
        
        NSLog(@"%@",error);
        
    }];
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

/** 数量 */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    //#warning Incomplete implementation, return the number of sections
    NSLog(@"%ld",self.wonderfulArray.count);
    return 1;
}

/** 行数 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.wonderfulArray.count;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 1;
}

/** 行高 */
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 510;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //    self.index = indexPath.row;
    /** 注册WoderfulTableViewCell */
    WoderfulTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"woderfulCell" forIndexPath:indexPath];
    /** 注册collectionImage */
    //[cell.collectionImage registerNib:[UINib nibWithNibName:@"WoderfulCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"celled"];
    //设置代理
    //cell.collectionImage.delegate = self;
    //cell.collectionImage.dataSource = self;
    
    //    NSLog(@"%ld",indexPath.section);
    
    WoderfulModel *model = self.wonderfulArray[indexPath.row];
    
    /** 头像 */
    [cell.heabImage sd_setImageWithURL:[NSURL URLWithString:model.headimg] placeholderImage:[UIImage imageNamed:@"5-1"] options:SDWebImageCacheMemoryOnly completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        
    }];
    
    cell.heabImage.layer.masksToBounds = YES;
    cell.heabImage.layer.cornerRadius = cell.heabImage.frame.size.width / 2;
    /** 时间 */
    cell.timeLabel.text = model.ctime;
    /** 名字 */
    cell.nameLabel.text = model.username;
    /** 寄语 */
    cell.remarksLabel.text = model.title;
    cell.oneImage.tag = 200+indexPath.row;
    cell.collectionImage.tag = 100+indexPath.row;
    
    if ([self.imageDic valueForKey:[NSString stringWithFormat:@"%ld",indexPath.row]] == nil) {
        [cell.oneImage sd_setImageWithURL:[NSURL URLWithString:model.urlArray[0]] placeholderImage:[UIImage imageNamed:@"5-1"] options:(SDWebImageCacheMemoryOnly)];
    }else{
        [cell.oneImage sd_setImageWithURL:[NSURL URLWithString:[self.imageDic valueForKey:[NSString stringWithFormat:@"%ld",indexPath.row]]] placeholderImage:[UIImage imageNamed:@"5-1"] options:(SDWebImageCacheMemoryOnly)];
        
    }
    
    
    
    return cell;
}


- (void)tableView:(UITableView *)tableView willDisplayCell:(WoderfulTableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //    if (indexPath.section == 0) {
    // 注册
    [cell.collectionImage registerNib:[UINib nibWithNibName:@"WoderfulCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"woderfulCell"];
    cell.collectionImage.delegate = self;
    cell.collectionImage.dataSource = self;
    cell.backgroundColor = [UIColor grayColor];
    // 刷新数据
    [cell.collectionImage reloadData];
    //    }
}

#pragma make --- cellectionView分区数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    WoderfulModel *model= [WoderfulModel new];
    
    model = self.wonderfulArray[[self.tableView indexPathForCell:(WoderfulTableViewCell *)[[collectionView superview] superview]].row];
    //
    //    NSLog(@"%ld",model.urlArray.count);
    return model.urlArray.count;
}


#pragma make --- cellectionView
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    //    NSLog(@"%ld %ld %ld",indexPath.section,indexPath.row,indexPath.item);
    
    WoderfulModel *model = self.wonderfulArray[[self.tableView indexPathForCell:(WoderfulTableViewCell *)[[collectionView superview] superview]].row];
    /** 注册 */
    UINib *nib = [UINib nibWithNibName:@"WoderfulCollectionViewCell" bundle:[NSBundle mainBundle]];
    /** 注册xib */
    [collectionView registerNib:nib forCellWithReuseIdentifier:@"woderfulCell2"];
    WoderfulCollectionViewCell *cell = [[WoderfulCollectionViewCell alloc]init];
    
    /** 设置标识符重用 */
    cell = [collectionView dequeueReusableCellWithReuseIdentifier: @"woderfulCell2" forIndexPath:indexPath];
    
    //    NSLog(@"%ld",indexPath.item);
    
    /** cellectionView赋值 */
    [cell.woderfulImage  sd_setImageWithURL:[NSURL URLWithString:model.urlArray[indexPath.item]] placeholderImage:[UIImage imageNamed:@"5-1"] options:SDWebImageCacheMemoryOnly completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        
    }];
    
    return cell;
    
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    UIImageView *imageview = (UIImageView*)[self.view viewWithTag:collectionView.tag+100];
    
    WoderfulModel *model = self.wonderfulArray[[self.tableView indexPathForCell:(WoderfulTableViewCell *)[[collectionView superview] superview]].row];
    
    [self.imageDic setValue:model.urlArray[indexPath.item] forKey:[NSString stringWithFormat:@"%ld",collectionView.tag-100]];
    [imageview  sd_setImageWithURL:[NSURL URLWithString:model.urlArray[indexPath.item]] placeholderImage:[UIImage imageNamed:@"5-1"] options:SDWebImageCacheMemoryOnly completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        
    }];
    
    
    
}






@end
